home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 27 / develop issue 27 code / 3d game controls / source / document.c < prev    next >
Encoding:
Text File  |  1996-06-29  |  4.1 KB  |  137 lines

  1. //--------------------------------------------------------------------------------------------
  2. //  Application Document Functions
  3. //
  4. //      by Philip McBride
  5. //
  6. //--------------------------------------------------------------------------------------------
  7.  
  8.  
  9. #include "GameControls.h"
  10. #include "extern.h"
  11. #include "document.h"
  12. #include "context.h"
  13. #include "camera.h"
  14. #include "lights.h"
  15.  
  16. //--------------------------------------------------------------------------------------------
  17. //  Create a new Document
  18. //
  19. DocumentPtr MyNewDocument()
  20. {
  21.     DocumentPtr             theDocument;
  22.     CWindowPtr                theWindow;
  23.     TQ3Status                myStatus;
  24.     TQ3RendererObject        myRenderer;
  25.     Rect                    theRect = { 0, 0, 16, 16 };
  26.     TQ3Param2D                uvValues = {-1.0, -1.0};
  27.     TQ3DrawContextObject    theDrawContext ;
  28.     Rect                    myBounds = kMyBoundsRect;
  29.     TQ3CameraObject            camera = NULL ;
  30.             
  31.     // create the document record
  32.     theDocument = (DocumentPtr )NewPtrClear(sizeof(DocumentRecord));
  33.     
  34.     // create the window to display the model and add referneces to the document
  35.     theWindow = (CWindowPtr)NewCWindow(0L,&myBounds,"\pModel Window",true,documentProc,(WindowPtr)-1L,true,NULL);
  36.     if (theWindow == nil)
  37.         goto bail;
  38.     theDocument->theWindow = theWindow;
  39.     SetWRefCon((WindowPtr)theWindow, (long)theDocument ) ;
  40.     
  41.     // add the window dimensions (width and height) to the document
  42.     theDocument->width = myBounds.right - myBounds.left;
  43.     theDocument->height = myBounds.bottom - myBounds.top;
  44.         
  45.     // zero out the file spec
  46.     theDocument->theFileSpec.vRefNum = 0 ;
  47.     theDocument->theFileSpec.parID = 0 ;
  48.     theDocument->theFileSpec.name[0] = '\0' ;
  49.     
  50.     // Create the new draw context
  51.     theDrawContext = MyNewDrawContext( (WindowPtr)theWindow ) ;
  52.     if (theDrawContext == NULL)
  53.         goto bail;
  54.     
  55.     // Create the view and assign the draw context
  56.     theDocument->theView = Q3View_New();
  57.     if (theDocument->theView == NULL)
  58.         goto bail;
  59.     if ((myStatus = Q3View_SetDrawContext(theDocument->theView, theDrawContext)) == kQ3Failure )
  60.         goto bail;
  61.     Q3Object_Dispose(theDrawContext);
  62.         
  63.     // Add more model and view properties to the document record
  64.     theDocument->documentGroup = nil;
  65.     theDocument->currentInterpolation = kQ3InterpolationStylePixel;
  66.     theDocument->illuminationShader = Q3PhongIllumination_New();
  67.  
  68.     // Use the interactive software renderer
  69.     if ((myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive)) != nil ) {
  70.         if ((myStatus = Q3View_SetRenderer(theDocument->theView, myRenderer)) == kQ3Failure ) {
  71.             goto bail;
  72.         }
  73.         // these two lines set us up to use the best possible renderer,
  74.         // including  hardware if it is installed.
  75.         Q3InteractiveRenderer_SetDoubleBufferBypass (myRenderer, kQ3True);                        
  76.         Q3InteractiveRenderer_SetPreferences(myRenderer, kQAVendor_BestChoice, 0);
  77.  
  78.     }
  79.     else {
  80.         goto bail;
  81.     }
  82.     Q3Object_Dispose( myRenderer ) ;
  83.  
  84.     // Setup the window gworld
  85.     SetGWorld(theWindow,nil);
  86.     
  87.     return(theDocument);
  88.     
  89. bail:
  90.     if (theWindow)
  91.         DisposeWindow((WindowPtr) theWindow);
  92.  
  93.     if (theDocument)
  94.         DisposePtr((Ptr)theDocument);        
  95.  
  96.     return( (DocumentPtr )nil );
  97. }
  98.  
  99.  
  100. //--------------------------------------------------------------------------------------------
  101. //  Delete a Document
  102. //
  103. void MyCloseDocument( DocumentPtr theDocument )
  104. {    
  105.     // if it has a file associated, close it
  106.     if (theDocument->fRefNum)
  107.         FSClose(theDocument->fRefNum);
  108.  
  109.     // and we can dispose of the window
  110.     if(theDocument->theWindow)
  111.         DisposeWindow((WindowPtr)theDocument->theWindow);
  112.  
  113.     // dispose of our QuickDraw 3d view object
  114.     if(theDocument->theView)
  115.         Q3Object_Dispose(theDocument->theView);
  116.  
  117.     // if we have a group associated with the document, then delete that
  118.     if(theDocument->documentGroup)
  119.         Q3Object_Dispose(theDocument->documentGroup);
  120.  
  121.     if(theDocument->illuminationShader)
  122.         Q3Object_Dispose(theDocument->illuminationShader);
  123.     
  124.     // finally dispose of the storage used for the document and 
  125.     // decrement the document count
  126.     if( theDocument != nil )
  127.         DisposPtr((Ptr) theDocument);
  128. }
  129.  
  130. //--------------------------------------------------------------------------------------------
  131. //  Find a Document from a Window
  132. //
  133. DocumentPtr MyGetDocumentFromWindow(theWindow)
  134. {
  135.     return (DocumentPtr)GetWRefCon((WindowPtr)theWindow);
  136. }
  137.